import java.awt.Point;
import java.awt.Rectangle;
import java.util.Random;

// when the query is a rect and respons is of type line
public class PLAttribRectLine extends PLAttribShapeLine {	
	private static final long serialVersionUID = 1L;

	public PLAttribRectLine() {
		super();
	}

	public float BuildResponse(PLShape inQuery, PLOutShape outResponse) {

		Random generator = new Random();

		PLLine query = (PLLine)inQuery;

		// quadrants
		int neg = (generator.nextInt(2) == 1) ? 1: -1;
		
		int startingQuadrand = (query.GetStartingQuadrant() + neg*GetStartingQuadrant() + 4) % 4;
		int endingQuadrand = (query.GetEndingQuardrant() + neg*GetEndingQuadrant() + 4) % 4;

		// starting point		
		Point sPoint =  new Point(generator.nextInt(PLGuiInfo.Obj().GetWidth()/2), generator.nextInt(PLGuiInfo.Obj().GetHeight()/2));		
		Rectangle sQuad = PLShapeUtils.GetQuadrantRect(startingQuadrand);
		sPoint.x += (int)sQuad.getX();
		sPoint.y += (int)sQuad.getY();		

		// length
		float length = query.GetLength()*GetLength();

		// slope
		float slopeAngle = query.GetSlopeAngle();	
		slopeAngle += (generator.nextInt(2) == 1) ? GetSlopeAngle(): -1* GetSlopeAngle();

		// end point
		float xDist = (float)(length * Math.cos(slopeAngle));
		float yDist = (float)(length * Math.sin(slopeAngle));
		

		//  +, +
		Point eBestPoint = new Point();
		eBestPoint.x = (int)(sPoint.x + xDist);
		eBestPoint.y = (int)(sPoint.y + yDist);

		int bestQuadDiff = Math.abs(PLShapeUtils.GetQuadrant(eBestPoint) - endingQuadrand);

		//  +, -
		Point temp = new Point((int)(sPoint.x + xDist), (int)(sPoint.y - yDist));
		int tempQuadDiff = Math.abs(PLShapeUtils.GetQuadrant(temp) - endingQuadrand);

		if (tempQuadDiff < bestQuadDiff || (tempQuadDiff == bestQuadDiff  && (generator.nextInt(2) == 1))) {
			eBestPoint.setLocation(temp);
			bestQuadDiff = tempQuadDiff;
		}
		
		//  -, -
		temp = new Point((int)(sPoint.x - xDist), (int)(sPoint.y - yDist));
		tempQuadDiff = Math.abs(PLShapeUtils.GetQuadrant(temp) - endingQuadrand);

		if (tempQuadDiff < bestQuadDiff || (tempQuadDiff == bestQuadDiff  && (generator.nextInt(2) == 1))) {
			eBestPoint.setLocation(temp);
			bestQuadDiff = tempQuadDiff;
		}

		//  -, +
		temp = new Point((int)(sPoint.x - xDist), (int)(sPoint.y + yDist));
		tempQuadDiff = Math.abs(PLShapeUtils.GetQuadrant(temp) - endingQuadrand);

		if (tempQuadDiff < bestQuadDiff || (tempQuadDiff == bestQuadDiff  && (generator.nextInt(2) == 1))) {
			eBestPoint.setLocation(temp);
			bestQuadDiff = tempQuadDiff;
		}

		outResponse.SetShape(new PLLine(sPoint, eBestPoint));
				
		return 1;
	}

	public void Update(PLShape inQuery, PLShape inResponse) {
		PLRect query = (PLRect)inQuery;
		PLLine response = (PLLine)inResponse;
		
		UpdateLength(query.GetLength(), response.GetLength());
		UpdateSlope(query.GetSlopeAngle(), response.GetSlopeAngle());
		UpdateStartingQuadrant(query.GetStartingQuadrant(), response.GetStartingQuadrant());
		UpdateEndingQuadrant(query.GetEndingQuardrant(), response.GetEndingQuardrant());	
	}
}
